<template>
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="女王卡订单号">
              <el-input v-model="form.order_sn" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖励人姓名/手机号">
              <el-input v-model="form.keyword" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="级别">
              <el-select v-model="form.award_type" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in awardsType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购卡类型">
              <el-select v-model="form.type" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in cardType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖励时间">
              <el-date-picker
                v-model="dates"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
                <Exprotcomponent :params="form" url="admin/qreward/award_export" type="php" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <el-table-column
          fixed
          type="index"
          label="编号"
          width="50"
          :index="table_index"
          align="center"
        />
        <el-table-column label="订单编号" prop="order_sn" align="center" width="150px" />
        <el-table-column label="姓名" prop="user_name" align="center" width="100px" />
        <el-table-column label="手机号" prop="user_phone" align="center" width="170px">
          <template slot-scope="scope">
            <span>{{scope.row.user_phone |phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="购卡类型" prop="type_name" align="center" width="120px" />
        <el-table-column label="奖励金额" prop="amount" align="center" width="120px" />
        <el-table-column label="奖励手机号" prop="reward_user_phone" align="center" width="175px">
          <template slot-scope="scope">
            <span>{{scope.row.reward_user_phone |phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="奖励姓名" prop="reward_user_name" align="center" width="120px" />
        <el-table-column label="奖励推荐人级别" prop="award_type_name" align="center" width="120px" />
        <el-table-column label="奖励详情" prop="msg" align="center" width="120px" />
        <el-table-column label="奖励时间" prop="created_at" align="center" width="180px">
          <template slot-scope="scope">
            <span>{{scope.row.created_at | time}}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reward, rewardLevel } from "@/api/goddessMange";
import commonFn from "@/utils/common";
export default {
  name: "rewardQueen",
  data() {
    return {
      form: {
        order_sn: "",
        keyword: "",
        type: "",
        reward_time_start: "",
        reward_time_end: "",
        award_type: "",
        page: 1,
        size: 10
      },
      dates: [],
      cardType: [
        {
          name: "全部",
          value: 0
        },
        {
          name: "女王卡",
          value: 1
        },
        {
          name: "青春卡",
          value: 2
        },
        {
          name: "轻颜卡",
          value: 3
        }
      ],
      awardsType: [],
      tableData: [],
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0,
      tableLoading: true
    };
  },
  filters: {
    time(opt) {
      if (opt != "") {
        return new Date(Number(opt) * 1000).format("yyyy-MM-dd hh:mm:ss");
      } else {
        return "--";
      }
    }
  },
  created() {
    this.rewardSelect();
    this.getList(this.form);
  },
  methods: {
    // 编号
    table_index(index) {
      return (this.form.page - 1) * this.form.size + index + 1;
    },
    // 列表
    getList(opt) {
      reward(opt)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.page_totals = res.data.meta.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.tableLoading = false;
          this.$message({
            message: err.msg,
            type: "error"
          });
        });
    },
    // 奖励级别下拉
    rewardSelect() {
      rewardLevel()
        .then(res => {
          if (res.data.code == 200) {
            let arr = [];
            res.data.data.forEach(item => {
              arr.push({
                name: item.name,
                value: item.id
              });
            });
            arr.unshift({ name: "全部", value: 0 });
            this.awardsType = arr;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.$message({
            message: err.msg,
            type: "error"
          });
        });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      if (this.dates != null) {
        this.form.reward_time_start = this.dates[0];
        this.form.reward_time_end = this.dates[1];
      } else {
        this.form.reward_time_start = null;
        this.form.reward_time_end = null;
      }
      this.getList(this.form);
    },
    // 当前页数
    handleSizeChange(val) {
      this.form.size = val;
      this.getList(this.form);
    },
    // 当前页
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    }
  }
};
</script>
<style scoped>
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
</style>
